package day20170417.ch01;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import utils.ArrayUtil;

public class PetDaoTest {

	/**
	 * 实例化宠物商店，才能拿到数组（笼子）
	 */
	private PetShop petShop = new PetShop();
	
	 /**
	  * 子类赋值给父类（多态）
	  */
	private PetDao dao = new PetDaoImpl();
	
	/**
	 * 在所有方法执行前执行
	 * @throws Exception
	 */
	@Before
	public void before() throws Exception {
		petShop.setName("华润超市");
		petShop.setPets(new Pet[10]);
		Pet[] pets = petShop.getPets();
		
		Cat jia = new Cat("加菲猫", 8,"红色");
		pets[0] = jia;
		Cat fly = new Cat("飞猫", 2,"绿色");
		pets[1] = fly;
		Cat dian = new Cat("闪电猫", 8,"红色");
		pets[2] = dian;
		Cat duo = new Cat("多啦A梦", 8,"蓝色");
		pets[3] = duo;
		Dog dog = new Dog("八公", 2,"白色");
		pets[4] = dog;
	}
	
	@Test
	public void updatePetAll() throws Exception {
		Pet before = new Cat("加菲猫",0,"红色");
		Pet after = new Cat(null,18,"绿色");
		Pet[] arryas = dao.updateAllPet(petShop, before, after);
		ArrayUtil.arrayPrint(arryas);
	}
	
	/**
	 * 增加一个宠物的测试和宠物名字查询
	 * 
	 * @throws Exception
	 */
	@Test
	public void addPetTest() throws Exception {
		// 调用工具类
		ArrayUtil.arrayPrint(petShop.getPets());
		// 调用查询方法，返回为空则为数组（笼子）没有这个对象
		Pet result = dao.findPetByName(petShop, "加菲猫");
		System.out.println(result);
	}

	/**
	 * 批量删除宠物
	 * 
	 * @throws Exception
	 */
	@Test
	public void deletePetTest() throws Exception {
		//调用删除方法，返回新的数组
		Pet[] deletePet = dao.deletePet(petShop);
		//调用工具类
		ArrayUtil.arrayPrint(deletePet);
	}
	
	/**
	 * 修改宠物
	 * @throws Exception
	 */
	@Test
	public void updatePetTest() throws Exception {
		Pet updatePet = dao.updatePet(petShop, "多啦A梦", 999);
		System.out.println(updatePet);
	}
	
	/**
	 * 宠物年龄查询
	 * 
	 * @throws Exception
	 */
	@Test
	public void findAgePetTest() throws Exception {
		// 调用查询方法，返回为空则为数组（笼子）没有这个对象
		Pet[] result = dao.findPetByAge(petShop, 8);
		// 调用工具类
		ArrayUtil.arrayPrint(result);
	}

}
